package com.infore.maintenance.main.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.TBaseGroup;
import com.infore.common.model.entity.vo.TBaseGroupVo;
import com.infore.maintenance.main.service.TBaseGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 模板分组 前端控制器
 * </p>
 *
 * @author lijh
 * @since 2023-01-11
 */
@RestController
@Api(value = "模板分组前端控制器", tags = "模板分组前端控制器")
@RequestMapping("/tBaseGroup")
public class TBaseGroupController extends BaseController {
	private String prefix = "/tBaseGroup";

	@Autowired
	private TBaseGroupService TBaseGroupService;

	@GetMapping()
	public String TBaseGroupController(){
		return prefix;
	}

	/**
	 * 查询模板分组列表
	 * @param tbasegroupVo 模板分组的vo类
	 * @return Msg<Object> 结果集
	 * @author lijh
	 * @since 2023-01-11
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "查询模板分组列表", notes = "模板分组列表查询 ")
	public Msg<Object> list(TBaseGroupVo tbasegroupVo){
		//sql查询条件
		Wrapper<TBaseGroup> wrapper = null ;
		try {
			wrapper = (Wrapper<TBaseGroup>) WrapperHelp.addCondition(new EntityWrapper<TBaseGroup>(), tbasegroupVo);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error(500,e.getMessage());
		}
		//查询方法
		Page<TBaseGroup> eppPage = new EppPage<TBaseGroup>(TBaseGroupService,tbasegroupVo,wrapper);
		return ResultUtil.successPage(TBaseGroupService.selectPage(eppPage, wrapper));
	}

	/**
	 * 查询模板分组列表
	 * @param tbasegroup 模板分组
	 * @return Msg<Object> 结果集
	 * @author lijh
	 * @since 2023-01-11
	 */
	@ResponseBody
	@ApiOperation(value = "添加模板分组", notes = "模板分组新增")
	@RequestMapping(value="/add",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public Msg<Object> add(@RequestBody TBaseGroup tbasegroup){
		Wrapper<TBaseGroup> wrapper = new  EntityWrapper<TBaseGroup>();
	    wrapper.eq("group_name", tbasegroup.getGroupName());
		List<TBaseGroup> entityList = TBaseGroupService.selectList(wrapper);
		if(entityList.size()>0) {
			return ResultUtil.error("重复添加");
		}
	//插入数据
		boolean flag = TBaseGroupService.insert(tbasegroup);
		return ResultUtil.setResult(flag,tbasegroup.getId().toString());
	}


	/**
	  * 更新模板分组方法
	  *
	  * @param tbasegroup 模板分组
	  * @return Msg<Object>
	  * @author lijh
	  * @since 2023-01-11
	  */
	@ResponseBody
	@ApiOperation(value = "更新模板分组", notes = "模板分组更新")
	@RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Msg<Object> edit(@RequestBody TBaseGroup tbasegroup) {
		//插入数据
		boolean flag = TBaseGroupService.updateById(tbasegroup);
		return ResultUtil.setResult(flag, tbasegroup.getId().toString());
	}

	/**
	 * 删除模板分组
	 *
	 * @param  id
	 * @return Msg<Object>
	 * @author lijh
	 * @since 2023-01-11
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "删除模板分组", notes = "删除模板分组")
	public Msg<Object> del(String id) {

		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("id", id);
		boolean flag = TBaseGroupService.deleteByMap(columnMap);
		return ResultUtil.setResult(flag);
	}
}
